package com.auth.authserver.controller;

import com.auth.authserver.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author Json
 * @date 2021/10/29 14:46
 */
@Slf4j
@RequestMapping
@RestController
public class LoginController {
    private UserService userService;

    public LoginController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/getUser")
    public String getUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        boolean authenticated = authentication.isAuthenticated();
        log.info("authenticated:" + authenticated);
        log.info("author:"+authentication.getAuthorities());
        log.info("getCredentials:"+authentication.getCredentials());
        log.info("getCredentials:"+authentication.getDetails());
        return "00";
    }

    @PreAuthorize("hasAuthority('user:resource1')")
    @GetMapping("/getResource")
    public String getResource(){
        return "资源";
    }

    @GetMapping("/getTest")
    public String getTest(){
        return userService.getTest();
    }


}
